{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "a64f1fe5",
   "metadata": {},
   "source": [
    "读取ply文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "f512a88f",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "8c0c8608",
   "metadata": {},
   "outputs": [],
   "source": [
    "def read_ply(filename):\n",
    "    \"\"\"读取简单的ASCII PLY文件\"\"\"\n",
    "    with open(filename, 'r') as f:\n",
    "        lines = f.readlines()\n",
    "    \n",
    "    # 找到顶点和面的数量\n",
    "    vertex_count = 0\n",
    "    face_count = 0\n",
    "    for line in lines:\n",
    "        if line.startswith('element vertex'):\n",
    "            vertex_count = int(line.split()[-1])\n",
    "        elif line.startswith('element face'):\n",
    "            face_count = int(line.split()[-1])\n",
    "        elif line.startswith('end_header'):\n",
    "            header_end = lines.index(line) + 1\n",
    "            break\n",
    "    \n",
    "    # 读取顶点数据\n",
    "    vertices = np.array([\n",
    "        list(map(float, line.split()[:3]))\n",
    "        for line in lines[header_end:header_end + vertex_count]\n",
    "    ])\n",
    "    \n",
    "    # 读取面数据\n",
    "    faces = np.array([\n",
    "        list(map(int, line.split()[1:4]))\n",
    "        for line in lines[header_end + vertex_count:header_end + vertex_count + face_count]\n",
    "    ])\n",
    "    \n",
    "    return vertices, faces"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "02a2de9a",
   "metadata": {},
   "outputs": [],
   "source": [
    "filepath = \"C:\\\\Users\\\\Henan\\\\Downloads\\\\Part1\\\\1-bottom.ply\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "db0862ac",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "顶点形状: (11919936, 3)\n",
      "面形状: (0,)\n"
     ]
    }
   ],
   "source": [
    "# 使用示例\n",
    "vertices, faces = read_ply(filepath)\n",
    "print(f\"顶点形状: {vertices.shape}\")\n",
    "print(f\"面形状: {faces.shape}\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "my_env",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
